package com.example.demojimmer.application.in.rest.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import com.example.demojimmer.application.dto.Results;
import com.example.demojimmer.application.services.LoginApplicationService;
import com.example.demojimmer.domain.userauth.model.entity.dto.LoginUserInput;
import lombok.RequiredArgsConstructor;
import org.babyfish.jimmer.client.meta.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

//@ApiIgnore
@Api
@RestController
@RequestMapping("/authorizeOapi")
@RequiredArgsConstructor
public class LoginController {
    //private final UserServiceApplicationService userServiceApplicationService;
    private final LoginApplicationService loginApplicationService;

    @PostMapping("/genLineCaptcha")
    public void genLineCaptcha(HttpServletResponse httpServletResponse) throws IOException {
        // 创建一个LineCaptcha对象，指定验证码的宽度、高度、位数和干扰线数量
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 45, 1, 4);

        // 设置验证码生成器为MathGenerator，用于生成加减乘除的运算验证码
        lineCaptcha.setGenerator(new MathGenerator());

        // 生成验证码
        lineCaptcha.createCode();

        // 获取验证码的文本内容
        String code = lineCaptcha.getCode();
        System.out.println("运算生成的验证码是: " + code);

        lineCaptcha.write(httpServletResponse.getOutputStream());
    }

    /**
     * 登录的接口
     *
     * @param loginUserInput
     * @return
     */


    @PostMapping("/login")
    public Results<List<LoginApplicationService.TokenVo>> doLogin(@RequestBody LoginUserInput loginUserInput) {

        return loginApplicationService.doLogin(loginUserInput);
    }

    /**
     * 刷新token接口;
     * 前端需要设置倒计时任务 在token失效前调用, 建议提前 10到30秒
     *
     * @return
     */
    @PostMapping("/refreshToken")
    public Results<List<LoginApplicationService.TokenVo>> refreshToken() {

        return loginApplicationService.refreshToken();
    }


    // private final ApplicationContext applicationContext;

    @PostMapping("/scanMethodsWithAnnotation")
    public Results<String> scanMethodsWithAnnotation() {

        List<String> permissions = new ArrayList<>();
        //扫描类使用hutool
        Set<Class<?>> classes1 = ClassUtil.scanPackage("com.example");
        for (Class candidate : classes1) {

            for (Method method : candidate.getDeclaredMethods()) {
                // 检查方法是否包含自定义注解
                if (method.isAnnotationPresent(PreAuthorize.class)) {
                    System.out.println("Found method with custom annotation: " + method.getName());
                    // 这里可以添加更多的逻辑，例如调用方法或记录方法信息
                    PreAuthorize annotation = method.getAnnotation(PreAuthorize.class);
                    String value = annotation.value();
                    String permission = StrUtil.subBetween(value, "'");

                    permissions.add(permission);
                }
            }

        }


        System.out.println("扫描到的注释");
        System.out.println(permissions);

        return new Results<String>();
    }
}

